package com.jgjc.service.serviceImpl;

import com.jgjc.dao.FrameMemberDao;
import com.jgjc.dao.FramingInfoDao;
import com.jgjc.entity.CommonEntity;
import com.jgjc.service.FrameMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FrameMemberServiceImpl implements FrameMemberService {

    @Resource
    FrameMemberDao frameMemberDao;

    @Resource
    FramingInfoDao framingInfoDao;

    @Override
    public List<Map<String, Object>> getList(int bridgeId, int sideTypeId) {
        List<Map<String, Object>> countMap = frameMemberDao.getCountStructs(bridgeId, sideTypeId);
        List<Map<String, Object>> mapList = frameMemberDao.getList(bridgeId, sideTypeId);
        if (mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                if (countMap.size() > 0) {
                    for (int j = 0; j < countMap.size(); j++) {
                        if (mapList.get(i).get("id") == countMap.get(j).get("siteNoId")) {
                            mapList.get(i).put("memberCount", countMap.get(j).get("memberCount"));
                            break;
                        } else {
                            mapList.get(i).put("memberCount", 0);
                        }
                    }
                } else {
                    mapList.get(i).put("memberCount", 0);
                }
            }
        }
        return mapList;
    }

    @Override
    public int addSite(List<Map<String, Object>> mapList, String bridgeSpanCombination, int id, int bridgeId, int sideTypeId) {
        List<Map<String, Object>> oldMapList = frameMemberDao.getList(bridgeId, sideTypeId);
        if (oldMapList.size() > 0) {
            for (int i = 0; i < oldMapList.size(); i++) {
                for (int j = 0; j < mapList.size(); j++) {
                    if (oldMapList.get(i).get("siteNo") == mapList.get(j).get("siteNo")) {
                        mapList.remove(j);
                    }
                }
            }
        }
        int isSiteAdd = frameMemberDao.addSite(mapList);
        if (isSiteAdd > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("bridgeSpanCombination", bridgeSpanCombination);
            map.put("id", id);
            return framingInfoDao.edit(map);
        } else {
            return isSiteAdd;
        }
    }

    @Override
    public List<Map<String, Object>> getListBySiteId(int siteId) {
        return frameMemberDao.getListBySiteId(siteId);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addSuperstructure(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddSuperstructure(idList);
        if (mapList.size() > 0) {
            frameMemberDao.deleteSuperstructure(idList);
        }
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    if (0 < (Integer) partsList.get(j).get("horizontalCount")) {
                        if (0 >= (Integer) partsList.get(j).get("verticalCount")) {
                            partsList.get(j).put("verticalCount", 1);
                        }
                        partsList.get(j).put("siteNoId", idList.get(i));
                        partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                        partsList.get(j).put("positionTypeId", CommonEntity.SBJGID);
                        if (null == partsList.get(j).get("structureTypeId")) {
                            partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                        }
                        code = frameMemberDao.addSuperstructure(partsList.get(j));
                    }
                }
            }
        }
        return code;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addPier(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddPier(idList);
        if (mapList.size() > 0) {
            frameMemberDao.deletePier(idList);
        }
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    if (0 < (Integer) partsList.get(j).get("horizontalCount") && !CommonEntity.TJC.equals(partsList.get(j).get("memberTypeName")) && !CommonEntity.DJC.equals(partsList.get(j).get("memberTypeName"))) {
                        if (0 >= (Integer) partsList.get(j).get("verticalCount")) {
                            partsList.get(j).put("verticalCount", 1);
                        }
                        partsList.get(j).put("siteNoId", idList.get(i));
                        partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                        partsList.get(j).put("positionTypeId", CommonEntity.XBJGID);
                        if (null == partsList.get(j).get("structureTypeId")) {
                            partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                        }
                        code = frameMemberDao.addSuperstructure(partsList.get(j));
                    } else {
                        if (0 >= (Integer) partsList.get(j).get("verticalCount")) {
                            partsList.get(j).put("verticalCount", 1);
                        }
                        partsList.get(j).put("siteNoId", idList.get(i));
                        partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                        partsList.get(j).put("positionTypeId", CommonEntity.XBJGID);
                        if (!CommonEntity.TJC.equals(partsList.get(j).get("memberTypeName"))) {
                            if (CommonEntity.DJC.equals(partsList.get(j).get("memberTypeName"))) {
                                partsList.get(j).put("structureTypeId", "");
                                if (null != ((List<Map<String, Object>>) requestMap.get("partsList")).get(0).get("materialTypeId")) {
                                    partsList.get(j).put("materialTypeId", ((List<Map<String, Object>>) requestMap.get("partsList")).get(0).get("materialTypeId"));
                                }
                                if (null == partsList.get(j).get("structureTypeId")) {
                                    partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                                }
                                partsList.get(j).put("horizontalCount", 1);
                                partsList.get(j).put("verticalCount", 1);
                                code = frameMemberDao.addSuperstructure(partsList.get(j));
                            }
                        }
                    }
                }
            }
        }
        return code;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addSiteRange(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddSiteRange(idList);
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (mapList.size() > 0) {
            if (26 == (int) partsList.get(0).get("partsTypeId")) {
                frameMemberDao.deleteRegulatingStructure(idList);
            } else {
                frameMemberDao.deleteSiteRange(idList);
            }

        }
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    partsList.get(j).put("horizontalCount", 1);
                    partsList.get(j).put("verticalCount", 1);
                    partsList.get(j).put("siteNoId", idList.get(i));
                    partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                    partsList.get(j).put("positionTypeId", CommonEntity.XBJGID);
                    if (null == partsList.get(j).get("structureTypeId")) {
                        partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                    }
                    code = frameMemberDao.addSuperstructure(partsList.get(j));
                }
            }
        }
        return code;
    }

    @Override
    public List<Map<String, Object>> getSuperstructureTypeList() {
        return frameMemberDao.getSuperstructureTypeList();
    }

    @Override
    public List<Map<String, Object>> getMaterialTypeList() {
        return frameMemberDao.getMaterialTypeList();
    }

    @Override
    public List<Map<String, Object>> getSupportTypeList() {
        return frameMemberDao.getSupportTypeList();
    }

    @Override
    public List<Map<String, Object>> getPierTypeList() {
        return frameMemberDao.getPierTypeList();
    }

    @Override
    public List<Map<String, Object>> getAbutmentTypeList() {
        return frameMemberDao.getAbutmentTypeList();
    }

    @Override
    public List<Map<String, Object>> getExpansionJointTypeList() {
        return frameMemberDao.getExpansionJointTypeList();
    }

    @Override
    public List<Map<String, Object>> getListByPier() {
        return frameMemberDao.getListByPier();
    }

    @Override
    public List<Map<String, Object>> getListByRiverbed() {
        return frameMemberDao.getListByRiverbed();
    }

    @Override
    public List<Map<String, Object>> getListByRegulatingStructure() {
        return frameMemberDao.getListByRegulatingStructure();
    }

    @Override
    public List<Map<String, Object>> getListByDeck() {
        List<Map<String, Object>> mapList = frameMemberDao.getListByDeck();
        List specialSectionList = new ArrayList<>();
        if (mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                mapList.get(i).put("specialSectionList", specialSectionList);
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getListByExpansionJoint() {
        return frameMemberDao.getListByExpansionJoint();
    }

    @Override
    public List<Map<String, Object>> getListBySuperstructure(int memberDescId) {
        return frameMemberDao.getListBySuperstructure(memberDescId);
    }

    @Override
    public List<Map<String, Object>> getListByAbutment(int memberDescId) {
        List<Map<String, Object>> mapList = frameMemberDao.getListByAbutment(memberDescId);
        List specialSectionList = new ArrayList<>();
        if (mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                mapList.get(i).put("specialSectionList", specialSectionList);
            }
        }
        return mapList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addAbutment(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddAbutment(idList);
        if (mapList.size() > 0) {
            frameMemberDao.deleteAbutment(idList);
        }
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    List specialSectionList = (List) partsList.get(j).get("specialSectionList");
                    if (0 < (int) partsList.get(j).get("horizontalCount") || specialSectionList.size() > 0 || CommonEntity.TJC.equals(partsList.get(j).get("memberTypeName"))) {
                        String specialSection = "";
                        if (specialSectionList.size() > 0) {
                            partsList.get(j).put("horizontalCount", specialSectionList.size());
                            partsList.get(j).put("verticalCount", 1);
                            for (int n = 0; n < specialSectionList.size(); n++) {
                                specialSection = specialSection + specialSectionList.get(n) + ",";
                            }
                            specialSection = specialSection.substring(0, specialSection.length() - 1);
                            partsList.get(j).put("specialSection", specialSection);
                        } else {
                            partsList.get(j).put("verticalCount", 1);
                        }
                        partsList.get(j).put("siteNoId", idList.get(i));
                        partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                        partsList.get(j).put("positionTypeId", CommonEntity.XBJGID);
                        if (CommonEntity.TJC.equals(partsList.get(j).get("memberTypeName"))) {
                            partsList.get(j).put("horizontalCount", 1);
                            partsList.get(j).put("structureTypeId", "");
                            if (null != ((List<Map<String, Object>>) requestMap.get("partsList")).get(0).get("materialTypeId")) {
                                partsList.get(j).put("materialTypeId", ((List<Map<String, Object>>) requestMap.get("partsList")).get(0).get("materialTypeId"));
                            }
                        }
                        if (null == partsList.get(j).get("structureTypeId")) {
                            partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                        }
                        code = frameMemberDao.addSuperstructure(partsList.get(j));
                    }
                }
            }
        }
        return code;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addDeck(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddDeck(idList);
        if (mapList.size() > 0) {
            frameMemberDao.deleteDeck(idList);
        }
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    if (CommonEntity.QMPZ.equals(partsList.get(j).get("memberTypeName"))) {
                        partsList.get(j).put("horizontalCount", 1);
                    }
                    List specialSectionList = (List) partsList.get(j).get("specialSectionList");
                    if (0 < (Integer) partsList.get(j).get("horizontalCount") || specialSectionList.size() > 0) {
                        if (0 >= (Integer) partsList.get(j).get("verticalCount")) {
                            partsList.get(j).put("verticalCount", 1);
                        }
                        String specialSection = "";
                        if (specialSectionList.size() > 0) {
                            for (int n = 0; n < specialSectionList.size(); n++) {
                                specialSection = specialSection + specialSectionList.get(n) + ",";
                            }
                            specialSection = specialSection.substring(0, specialSection.length() - 1);
                            partsList.get(j).put("horizontalCount", specialSectionList.size());
                        }
                        partsList.get(j).put("specialSection", specialSection);
                        partsList.get(j).put("siteNoId", idList.get(i));
                        partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                        partsList.get(j).put("positionTypeId", CommonEntity.QMXID);
                        if (null == partsList.get(j).get("structureTypeId")) {
                            partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                        }
                        if (!CommonEntity.SSFZZ.equals(partsList.get(j).get("partsTypeName")) && !CommonEntity.SSF.equals(partsList.get(j).get("partsTypeName"))) {
                            code = frameMemberDao.addSuperstructure(partsList.get(j));
                        }
                    }
                }
            }
        }
        return code;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addExpansionJoint(List siteNoList, Map<String, Object> requestMap) {
        List idList = frameMemberDao.getIdListBySiteAndBS(siteNoList, (Integer) requestMap.get("bridgeId"), (Integer) requestMap.get("sideTypeId"));
        List<Map<String, Object>> mapList = frameMemberDao.isAddExpansionJoint(idList);
        if (mapList.size() > 0) {
            frameMemberDao.deleteExpansionJoint(idList);
        }
        int code = 0;
        List<Map<String, Object>> partsList = (List<Map<String, Object>>) requestMap.get("partsList");
        if (idList.size() > 0 && partsList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                for (int j = 0; j < partsList.size(); j++) {
                    partsList.get(j).put("horizontalCount", 1);
                    partsList.get(j).put("verticalCount", 1);
                    partsList.get(j).put("siteNoId", idList.get(i));
                    partsList.get(j).put("positionTypeName", requestMap.get("BUWEI"));
                    partsList.get(j).put("positionTypeId", CommonEntity.QMXID);
                    if (null == partsList.get(j).get("structureTypeId")) {
                        partsList.get(j).put("structureTypeId", requestMap.get("structureTypeId"));
                    }
                    code = frameMemberDao.addSuperstructure(partsList.get(j));
                }
            }
        }
        return code;
    }

    @Override
    public int deleteStructInfoById(int id) {
        return frameMemberDao.deleteStructInfoById(id);
    }

    @Override
    public int edit(Map<String, Object> requestMap) {
        return frameMemberDao.edit(requestMap);
    }

}